{include file="Layout/header"/}
<body>
<div class="layui-fluid">
  <div class="layui-row layui-col-space15">
    <!--<div class="layui-col-sm12 layui-col-md3 layui-col-lg2">-->
    <!--  <div class="layui-card">-->
    <!--    <div class="layui-card-body mini-bar">-->
    <!--      <div class="ztree" id="deptTree"></div>-->
    <!--    </div>-->
    <!--  </div>-->
    <!--</div>-->
    <div class="layui-col-sm12 layui-col-md9 layui-col-lg12">
      <div class="layui-card">
        <div class="layui-card-body">
          <div class="layui-form toolbar">
            <div class="layui-form-item">
              <div class="layui-inline">
                <input id="name" class="layui-input layui-input-inline" type="text" placeholder="账号/姓名/手机号"/>
              </div>
              <div class="layui-inline">
                <input id="timeLimit" class="layui-input layui-input-inline" type="text" placeholder="注册时间"/>
              </div>
              <div class="layui-inline">
                <button id="btnSearch" class="layui-btn layui-btn-sm layui-btn-radius icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
                <button id="btnExp" class="layui-btn layui-btn-sm layui-btn-radius icon-btn"><i class="layui-icon">&#xe67d;</i>导出</button>
              </div>
            </div>
          </div>
          <table class="layui-table" id="userTable" lay-filter="userTable"></table>
        </div>
      </div>
    </div>
  </div>
</div>
<script type="text/html" id="userTableToolBar">
  {php} if(in_array("admin/sysuser/add",session('menunames'))){ {/php}
  <a class="layui-btn layui-btn-normal layui-btn-sm layui-btn-radius" lay-event="add"><i class="layui-icon">&#xe654;</i>添加</a>
  {php} } {/php}
  {php} if(in_array("admin/sysuser/edit",session('menunames'))){ {/php}
  <a class="layui-btn layui-btn-normal layui-btn-sm layui-btn-radius" lay-event="edit"><i class="layui-icon">&#xe642;</i>修改</a>
  {php} } {/php}
  {php} if(in_array("admin/sysuser/delete",session('menunames'))){ {/php}
  <a class="layui-btn layui-btn-danger layui-btn-sm layui-btn-radius" lay-event="del"><i class="layui-icon">&#xe640;</i>删除</a>
  {php} } {/php}
  {php} if(in_array("admin/sysuser/reset",session('menunames'))){ {/php}
  <a class="layui-btn layui-btn-green layui-btn-sm layui-btn-radius" lay-event="reset"><i class="layui-icon">&#xe673;</i>重置密码</a>
  {php} } {/php}
</script>
<script type="text/html" id="userTableOptions">
  <input type="checkbox" lay-filter="status" value="{{d.userId}}" lay-skin="switch" lay-text="正常|冻结" {{d.status=='ENABLE'?'checked':''}}/>
</script>
{include file="Layout/commonjs"/}
<script>
    layui.use(['layer', 'form', 'table', 'formSelects'], function () {
        layui.table.render({
            elem: "#userTable",
            id: 'userTable',
            url: '{:url("/admin/sysuser/view")}',
            method: "post",
            request: {
                limitName: 'pageSize'
            },
            page: true,
            //skin: 'line',
            //even: true, //开启隔行背景
            size: 'sm', //小尺寸的表格
            limit: '5',
            toolbar: '#userTableToolBar',
            loading: true,
            height: 'full-160',
            cellMinWidth: 100,
            parseData: function (res) {
                return {
                    "code": 0, //解析接口状态
                    "msg": '', //解析提示文本
                    "count": res.total, //解析数据长度
                    "data": res.data //解析数据列表
                }
            },
            cols: [[
                {type: 'radio'},
                {
                    field: "account",
                    title: '账号',
                    width: 100,
                },
                {
                    field: "nickname",
                    title: '昵称',
                    width: 100,
                },
                {
                    field: "sex",
                    title: '性别',
                    width: 70,
                    align: 'center',
                    templet: function (val) {
                        if (val.sex == 1) {
                            return "男";
                        } else {
                            return "女";
                        }
                    }
                },
                {
                    field: "email",
                    title: '邮件',
                    width: 150,
                },
                {
                    field: "mobile",
                    title: '电话',
                    width: 120,
                },
                {
                    field: "create_time",
                    title: '创建时间',
                    width: 160,
                    templet: function (val) {
                        if(val.create_time!=null&&val.create_time!=""){
                            return top.formatDateTime(val.create_time);
                        }else{
                            return "空";
                        }
                    }
                },
                {
                    field: "update_time",
                    title: '修改时间',
                    width: 160,
                    templet: function (val) {
                        if(val.update_time!=null&&val.update_time!=""){
                            return top.formatDateTime(val.update_time);
                        }else{
                            return "空";
                        }
                    }
                },
                {
                    field: "roles",
                    title: '所属角色',
                    width: 200,
                    templet: function (d) {
                        var tempstr = [];
                        $.each(d.roles, function (index, item) {
                            tempstr.push(item.title);
                        });
                        return tempstr.join(',');
                    }
                },
                {
                    field: "status",
                    title: '状态',
                    width: 70,
                    templet: function (val) {
                        if (val.status == "1") {
                            return '<span class="layui-badge layui-bg-green">启用</span>';
                        } else {
                            return '<span class="layui-badge layui-bg-red">禁用</span>';
                        }
                    }
                },
                {
                    field: "remark",
                    title: '备注',
                    align: 'left',
                },
                {field: "", title: '操作', toolbar: "#tableToolBar", width: 220, fixed: 'right', align: 'center', hide: true},
            ]],
            done: function (res, curr, count) {
            }
        })
        layui.table.on('toolbar(userTable)', function (obj) {
            var data = layui.table.checkStatus(obj.config.id).data[0];
            if (obj.event == 'add') {
                $.get('{:url("/admin/sysuser/add")}', {}, function (str) {
                    var indexlayer = layui.layer.open({
                        title: "添加",
                        offset: 't',
                        area: ['530px', '500px'],
                        shadeClose: true,
                        type: 1,
                        anim: 2,
                        resize: false,
                        btn: ['保存', '取消'],
                        content: str,
                        yes: function (index, layero) {
                            //return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
                            var data = $("#userForm").serializeObject();
                            var roleIds = layui.formSelects.value('roleIdsSelect', 'val');        // ["2","4"]
                            console.log(roleIds);
                            data.roleIds = roleIds;
                            $.ajax({
                                async: true,//默认true为异步
                                url: '{:url("/admin/sysuser/add")}',
                                type: "post",
                                data: data,
                                dataType: "json",
                                beforeSend: function (xhr) {
                                },
                                success: function (res, status, xhr) {
                                    layer.msg(res.msg);
                                    if (res.code == 1) {
                                        layui.table.reload('userTable', {});
                                        //layui.layer.close(indexlayer);
                                    }
                                },
                                error: function (xhr, status, error) {
                                }
                            });
                        },
                        success: function (layero, index) {
                        },
                        btn2: function (index, layero) {
                            //按钮【按钮二】的回调
                            //return false 开启该代码可禁止点击该按钮关闭
                        },
                        btn3: function (index, layero) {
                            //按钮【按钮三】的回调
                            //return false 开启该代码可禁止点击该按钮关闭
                        },
                        cancel: function () {
                            //右上角关闭回调
                            //return false 开启该代码可禁止点击该按钮关闭
                        }
                    })
                });
            }
            if (obj.event == 'edit') {
                if (data != null) {
                    $.get('{:url("/admin/sysuser/edit")}', {"id": data.id}, function (str) {
                        var indexlayer = layui.layer.open({
                            title: "修改",
                            offset: 't',
                            area: ['530px', '500px'],
                            shadeClose: true,
                            type: 1,
                            anim: 2,
                            resize: false,
                            btn: ['保存', '取消'],
                            content: str,
                            yes: function (index, layero) {
                                //return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
                                var data = $("#userForm").serializeObject();
                                var roleIds = layui.formSelects.value('roleIdsSelect', 'val');        // ["2","4"]
                                console.log(roleIds);
                                data.roleIds = roleIds;
                                $.ajax({
                                    async: true,//默认true为异步
                                    url: '{:url("/admin/sysuser/edit")}',
                                    type: "post",
                                    data: data,
                                    dataType: "json",
                                    beforeSend: function (xhr) {
                                    },
                                    success: function (res, status, xhr) {
                                        layer.msg(res.msg);
                                        if (res.code == 1) {
                                            layui.table.reload('userTable', {});
                                            layer.close(indexlayer);
                                        }
                                    },
                                    error: function (xhr, status, error) {
                                    }
                                });
                            },
                            success: function (layero, index) {
                            },
                            btn2: function (index, layero) {
                                //按钮【按钮二】的回调
                                //return false 开启该代码可禁止点击该按钮关闭
                            },
                            btn3: function (index, layero) {
                                //按钮【按钮三】的回调
                                //return false 开启该代码可禁止点击该按钮关闭
                            },
                            cancel: function () {
                                //右上角关闭回调
                                //return false 开启该代码可禁止点击该按钮关闭
                            }
                        })
                    });
                } else {
                    layer.msg('请勾选记录！');
                }
            }
            if (obj.event == 'del') {
                if (data != null) {
                    layui.layer.confirm('确定移除此信息？', {icon: 2, title: '提示信息'}, function (index) {
                        $.ajax({
                            type: "post",
                            url: '{:url("/admin/sysuser/delete")}',
                            data: {'id': data.id},
                            dataType: 'json',
                            async: false,
                            success: function (d) {
                                if (d.code == 200) {
                                    layui.layer.msg("删除成功");
                                    layui.table.reload('userTable', {});
                                } else {
                                    layui.layer.msg(d.msg);
                                }
                            }
                        })
                        layer.close(index);
                    });
                } else {
                    layer.msg('请勾选记录！');
                }
            }
            if (obj.event == 'reset') {
                console.log(loginuser);
                if (data != null) {
                    if (data.username == "admin" && loginuser.username != "admin") {
                        layer.msg('只有超级管理员用户可以修改admin密码！');
                    } else {
                        layer.prompt({
                            formType: 2,
                            value: '',
                            title: '请输入',
                            area: ['400px', '30px'] //自定义文本域宽高
                        }, function (value, index, elem) {
                            $.ajax({
                                type: "post",
                                url: '{:url("/admin/sysuser/reset")}',
                                data: {'id': data.id, 'password': value},
                                dataType: 'json',
                                async: false,
                                success: function (d) {
                                    if (d.code == 200) {
                                        layui.layer.msg("成功");
                                        layui.table.reload('userTable', {});
                                    } else {
                                        layui.layer.msg(d.msg);
                                    }
                                }
                            })
                            layer.close(index);
                        });
                    }
                } else {
                    layer.msg('请勾选记录！');
                }
            }
        });
    });
</script>
</body>
</html>